import {platformEnum, payChannelEnum, payChannelNameEnum} from '@/common/const.js'
import {storageKeyEnum} from '@/common/const.js'

const platform = {
	state: {
		platform: uni.getStorageSync(storageKeyEnum.platform),
		payChannel:uni.getStorageSync(storageKeyEnum.payChannel)
	},
	
	mutations:{
		SET_PLATFORM(state, platform){
			state.platform = platform
			uni.setStorageSync(storageKeyEnum.platform,platform)
		},
		
		SET_PAY_CHANNEL(state, payChannel){
			state.payChannel = payChannel
			uni.setStorageSync(storageKeyEnum.payChannel,payChannel)
		}
	},
	
	actions: {
		// 获取对应的平台
		getPlatform({commit}){
			return new Promise((resolve, reject)=>{
				let payChannels = []
				// 微信小程序
				// #ifdef MP-WEIXIN
				commit('SET_PLATFORM', platformEnum.wxApplet)
				payChannels.push({
					key:payChannelEnum.wxApplet,
					value:payChannelNameEnum.wxApplet
				})
				// #endif
				
				// 支付宝小程序
				// #ifdef MP-ALIPAY
				commit('SET_PLATFORM', platformEnum.aliApplet)
				//commit('SET_PAY_CHANNEL', [payChannelEnum.aliApplet])
				// #endif
				
				// #ifdef APP-PLUS
				let platform uni.getSystemInfoAsync().platform
				if(platform === 'ios'){
				  // 苹果
					commit('SET_PLATFORM', platformEnum.ios)
				} else if(platform === 'android'){
				  // 安卓
					commit('SET_PLATFORM', platformEnum.android)
				} else {
				  // 其他
				}
				//commit('SET_PAY_CHANNEL', [payChannelEnum.wxApp, payChannelEnum.aliApp])
				// #endif
				
				// #ifdef H5
				var ua = navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) && ua.match(/MicroMessenger/i)[0] === 'micromessenger') {
				  // 在微信中打开
					commit('SET_PLATFORM', platformEnum.wxH5)
					//commit('SET_PAY_CHANNEL', [payChannelEnum.wxJsapi])
				} else if (ua.match(/alipay/i) && ua.match(/alipay/i)[0] === 'alipay'){
					// 在支付宝打开
					commit('SET_PLATFORM', platformEnum.aliH5)
					//commit('SET_PAY_CHANNEL', [payChannelEnum.aliJsapi])
				} else {
					// 其他浏览器打开
					commit('SET_PLATFORM', platformEnum.H5)
					//commit('SET_PAY_CHANNEL', [payChannelEnum.wxJsapi, payChannelEnum.aliJsapi])
				}
				payChannels.push({
					key:payChannelEnum.wxApplet,
					value:payChannelNameEnum.wxApplet
				})
				// #endif
				
				// 添加钱包余额支付
				// payChannels.push({
				// 	key:payChannelEnum.wallet,
				// 	value:payChannelNameEnum.wallet
				// })
				commit('SET_PAY_CHANNEL', JSON.stringify(payChannels))
				resolve(true)
			})
			
		},
	}
}

export default platform